<?php

namespace MyApp\Controller;

use R as R;
use Lib\Annotations\Route as Route;
use Lib\Annotations\Middleware as Middleware;

/**
 * @Route("/")
 */
class HomeController extends \SlimController\SlimController {
    /**
     * 
     * @Route("/", name="index")
     * @Middleware({
     *      "userRequired",
     *      "authCallback"
     * })
     */
    public function indexAction()
    {
        $uuid = $this->app->request->get('uuid');
        $user = $this->app->user;
//        if (empty($user->coupon)) {
//            $coupon = R::findOne('coupon','uid = 0');
//            $coupon->uid = $user->id;
//            
//            $user->coupon = "" . $coupon->num;
//            R::store($coupon);
//            R::store($user);  
//        }
//        if (!$uuid) {
//            $this->redirect($this->app->urlFor('index') . '?uuid=' . $user->uuid);
//        } else if ($uuid != $user->uuid) { // 访问别人分享的授权回来的
//            $this->redirect($this->app->urlFor('friend', ['uuid' => $uuid]) . '?uuid=' . $user->uuid);
//        }
        $this->render('home/index');
    }

    /**
     * @Route("/submit", name="submit")
     * @Middleware({
     *     "userRequired"
     * })
     */
    public function submitAction() {
        $app = $this->app;
        $user = $app->user;
        
        $req = $app->request;
            
        $user->stuffArr   = trim($req->post('stuffArr')); 
        $user->score   = trim($req->post('score'));
        $user->updatedAt = R::isoDateTime();
        
        R::store($user);    
        $app->contentType('application/json');
        $app->response->write(json_encode(['ok'=> true]));
        return $app->response;
        exit;
        
    }
    
    /**
     * @Route("/mask", name="mask")
     */
    public function maskAction()
    {
        $this->render('home/mask');
    }
    /**
     * 
     * @Route("/friend/:uuid", name="friend")
     * @Middleware({
     *      "userRequired",
     *      "friendVisitable"
     * })
     */
    public function friendAction($uuid) 
    {
        $app = $this->app;
        $req = $this->app->request;
        $user = $this->app->user;
        // uid:所有分享出去的链接带的分享者的id
        $friend = R::findOne('user', ' uuid = ? ', [ $uuid ]);
        if (!$friend || $friend->id == $user->id) {
            $this->redirect($this->app->urlFor('index') . '?uuid=' . $user->uuid);
        }
        $friendship = R::findOne('friendship', 'uid = ? AND fid = ? ORDER BY id DESC LIMIT 1', [ $friend->id, $user->id ]);
        if($friendship->added) {
            $this->redirect($this->app->urlFor('index') . '?uuid=' . $user->uuid);
        }
        $stuffArr = explode(',', $friend->stuffArr);
        $impression = floor($friend->score / 40) + 1;
        $impression = $impression > 5 ? 5 : $impression;
        $this->render('home/friend',['friend' => $friend, 'stuffArr' => $stuffArr, 'impression' => $impression]);
    }
    /**
     * @Route("/test", name="test")
     */
    public function testAction()
    {
        $req = $this->app->request;
        $this->render('home/test',['kms' => $req->get('kms')]);
    }
    /**
     * @Route("/test1", name="test1")
     */
    public function test1Action()
    {
        $this->render('home/test1');
    }
}